package problem46;

//46.全排列
//https://leetcode.cn/problems/permutations/

import java.util.ArrayList;
import java.util.List;

class Solution {
    boolean[] st;
    List<List<Integer>> ret = new ArrayList<>();
    List<Integer> list = new ArrayList<>();
    public List<List<Integer>> permute(int[] nums) {
        st = new boolean[nums.length];
        dfs(nums, 0);
        return ret;
    }
    public void dfs(int[] nums, int u) {
        if(u == nums.length) {
            ret.add(new ArrayList(list));
            return;
        }
        for(int i = 0; i<nums.length; i++) {
            if(!st[i]) {
                list.add(nums[i]);
                st[i] = true;
                dfs(nums, u+1);
                st[i] = false;
                list.remove(list.size()-1);
            }
        }
    }
}